package com.yc.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class ZanBiz {

    public void add(String movieid,String userid){
        Jedis jedis = new Jedis("127.0.0.1",6379);
        try {
            String key = "zan_"+movieid;
            jedis.sadd(key,userid);
        }finally {
            jedis.close();
        }
    }

    private JedisPool jedisPool = new JedisPool("127.0.0.1",6379);

    public void del(String movieid,String userid){
        try (Jedis jedis = jedisPool.getResource()){
                String key = "zan_"+movieid;
                jedis.srem(key,userid);
        }
    }

    //判断是否已经点赞
    public  boolean isAlready(String movieid,String userid){
        try (Jedis jedis = jedisPool.getResource()){
            String key = "zan_"+movieid;
            return jedis.sismember(key,userid);
        }
    }

    public static void main(String[] args) {
        ZanBiz zanBiz = new ZanBiz();
        zanBiz.add("复联4","张三");
        zanBiz.add("复联4","李四");
        zanBiz.add("复联4","张三");

        System.out.println(zanBiz.isAlready("复联4","张三"));
        System.out.println(zanBiz.isAlready("复联4","李四"));
    }
}
